<template>
    <div>
        <div>
            <div class="table-operator">
                <a-button @click="setAuth()" type="primary" size="small">
                    {{i18n('isButton.empowerEditor','授权编辑') }}
                </a-button>
            </div>
        </div>
        <a-table :columns="columns" :data-source="authData" :pagination="false">
                <span slot="action" slot-scope="text, record">
                    <a-button type="link" @click="delAuth(record)" size="small">
                        {{$ti18('rxList.delBtn','删除')}}
                    </a-button>
                </span>
        </a-table>
    </div>
</template>

<script>
import OsGroupApi from "@/api/user/org/osGroup";
import {Dialog} from "@lowcode/jpaas-common-lib";


export default {
    name: "os-group-auth",
    props:{
        //传入用户组ID
        groupId:String
    },
    data(){
        return {
            columns:[{
                title: this.i18n('isTab.empowerRole','授权角色') ,
                dataIndex: 'groupName',
                width: 100,
            },
            {
                    title: this.$ti18('rxList.action','操作') ,
                    dataIndex: 'action',
                    width: 100,
                    scopedSlots: {customRender: 'action'}
            }],
            //授权数据
            authData:[]

        }

    },
    created(){
        this.init();
    },
    methods:{
        i18n(name,text,key){
            return this.$ti18(name,text,"osGroup",key);
        },
        init(){
            if(!this.groupId){
                return;
            }
            OsGroupApi.getByCompanyId(this.groupId).then(res=>{
                this.authData=res;
            });
        },
        delAuth(record){
            let self_=this;
            this.$confirm({
                title: this.$ti18('rxMsa.tipsInfo','提示信息') ,
                content: this.i18n('form.deleteEmpower','确认删除授权吗?'),
                okText: this.$ti18('rxForm.ackBtn','确认'),
                cancelText: this.$ti18('rxForm.cancelBtn','取消'),
                onOk() {
                    OsGroupApi.delAuth({ids: record.id}).then(res=>{
                        OsGroupApi.getByCompanyId(self_.groupId).then(res=>{
                            self_.authData=res;
                        });
                    })
                },
                onCancel() {},
            });

        },
        setAuth(){
            //
            let conf={
                curVm: this,
                data:{
                    single:false,
                    dimId:'2',
                    showDimId:false,
                    tenantId:this.tenantId
                }, widthHeight: ['1000px','600px']};
            let self_=this;
            Dialog.openGroupDialog(conf,function (obj,data){
                let aryJson=data.map(item=>{
                    return {id:item.groupId,name:item.name};
                })
                let json={companyId: self_.groupId,groups:aryJson};
                OsGroupApi.saveAuth(json).then(res=>{
                    OsGroupApi.getByCompanyId(self_.groupId).then(res=>{
                        self_.authData=res;
                    });
                });

            });
        }
    },
    watch:{
        groupId:function (val,oldVal){
            this.init();
        }
    }

}
</script>

<style scoped>

</style>